package com.peak.distribution.entity.admin;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;
import org.hibernate.validator.constraints.NotBlank;

import com.peak.distribution.enums.Constants;
import com.peak.spring.boot.api.entity.DefaultStategyEntity;

import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "dis_user")
@Getter
@Setter
public class DisUser extends DefaultStategyEntity {

	private static final long serialVersionUID = Constants.DISTRIBUTION_VERSION;
	
	/** 登录名 */
	@NotBlank
	private String username;

	/** 密码 */
	@NotBlank
	private String password;

	/** 状态，默认有效 */
	private Boolean status = true;
	
	/** 测试帐号 */
	private Boolean isTest;

	/** 最后一次登录时间 */
	private Date lastLoginDateTime;

	/** 登录的次数统计 */
	private Integer loginCount = 0;

	/** 是否需要密码，默认不需要，重置密码的时候设置为Y */
	private Boolean needModifyPassword = false;
	
	private DisRole role;

	@ManyToOne
	@JoinColumn(name = "role_id")
	@Fetch(FetchMode.SELECT)
	@LazyToOne(LazyToOneOption.PROXY)
	public DisRole getRole() {
		return role;
	}
	
	public void addLoginCount() {
		this.loginCount = this.loginCount == null ? 0 : this.loginCount;
		this.loginCount++;
	}
}
